package com.example.springbootdemo.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * mybatis-plus自动填充
 * @author 84246
 */
@Slf4j
@Component
public class MyPlusMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        //填充创建时间 更新时间
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
        //填充创建人 更新人,一般从上下文获取
        String userCode ="maopu";
        this.strictInsertFill(metaObject, "createBy", String.class, userCode);
        this.strictInsertFill(metaObject, "updateBy", String.class, userCode);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        //填充更新时间
        this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
        //填充更新人,一般从上下文获取
        String userCode = "maopu";
        //填充更新人
        this.strictUpdateFill(metaObject, "updateBy", String.class, userCode);
    }
}

